// calendar.js 日历逻辑
import {
  http
} from '../../utils/requestUtil'
Page({
  data: {
    currentYear: new Date().getFullYear(),
    currentMonth: new Date().getMonth() + 1,
    weekdays: ['日', '一', '二', '三', '四', '五', '六'],
    days: [],
    // 存储日期文字内容 { '2025-06-07': '会议记录' }
    dateTexts: {
      // '2025-06-17': '会议记录',
      // '2025-06-13': '12'
    }
  },



  // 生成日历数据
  generateCalendar() {
    const {
      currentYear,
      currentMonth
    } = this.data;
    const days = [];

    // 获取当月第一天和最后一天
    const firstDay = new Date(currentYear, currentMonth - 1, 1);
    const lastDay = new Date(currentYear, currentMonth, 0);

    // 上个月最后几天
    const prevMonthDays = firstDay.getDay(); // 第一天是周几
    for (let i = prevMonthDays - 1; i >= 0; i--) {
      const date = new Date(currentYear, currentMonth - 1, -i);
      days.push(this.createDayData(date, false));
    }

    // 当月所有天
    for (let i = 1; i <= lastDay.getDate(); i++) {
      const date = new Date(currentYear, currentMonth - 1, i);
      days.push(this.createDayData(date, true));
    }

    // 下个月前几天（补齐最后一行）
    const nextMonthDays = 42 - days.length; // 6行*7天=42个格子
    for (let i = 1; i <= nextMonthDays; i++) {
      const date = new Date(currentYear, currentMonth, i);
      days.push(this.createDayData(date, false));
    }

    this.setData({
      days
    });
  },

  // 创建日期数据对象
  createDayData(date, isCurrentMonth) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const weekday = date.getDay();
    const fullDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;

    // 检查是否是今天
    const today = new Date();
    const isToday = year === today.getFullYear() &&
      month === today.getMonth() + 1 &&
      day === today.getDate();

    // 从存储中获取该日期的文字内容
    const text = this.data.dateTexts[fullDate] || '';

    return {
      year,
      month,
      date: day,
      weekday,
      fullDate,
      isCurrentMonth,
      isToday,
      text
    };
  },

  // 加载已保存的文字内容
  loadDateTexts() {
    // const dateTexts = wx.getStorageSync('dateTexts') || {};
    // this.setData({ dateTexts });
  },

  // 保存文字内容到缓存
  saveDateTexts() {
    wx.setStorageSync('dateTexts', this.data.dateTexts);
  },

  // 选择日期
  selectDate(e) {
    console.log(e.currentTarget.dataset.date)
    const fullDate = e.currentTarget.dataset.date;
    wx.navigateTo({
      url: `/pages/index/edit/edit?date=${fullDate}`
    });
  },

  // 上个月
  prevMonth() {
    let {
      currentYear,
      currentMonth
    } = this.data;
    if (currentMonth === 1) {
      currentYear--;
      currentMonth = 12;
    } else {
      currentMonth--;
    }
    this.setData({
      currentYear,
      currentMonth
    }, () => {
      this.generateCalendar();
    });
  },

  // 下个月
  nextMonth() {
    let {
      currentYear,
      currentMonth
    } = this.data;
    if (currentMonth === 12) {
      currentYear++;
      currentMonth = 1;
    } else {
      currentMonth++;
    }
    this.setData({
      currentYear,
      currentMonth
    }, () => {
      this.generateCalendar();
    });
  },
  async loadData() {
    const res = await http.get("/memo/schedule/userlist")
    console.log(res.rows)


    let newData = this.data.dateTexts;
    // }
    // 1. 复制原有数据
    for (let i = 0; i < res.rows.length; i++) {
      newData[`${res.rows[i].scheduleDate}`] = res.rows[i].title
    }
    // newData['2025-06-17']="fefe"
    // const originalData = { ...this.data.dateTexts };

    // 2. 合并新数据（不会覆盖原有数据）
    // const updatedData = { ...originalData, ...newData };

    // 3. 更新到页面数据
    this.setData({
      dateTexts: newData
    });
    // this.setData({dateTexts[res.rows.scheduleDate]})
  },
  // onShow() {
  //   // this.loadData()
  // },
  onShow() {
    this.loadData()
    setTimeout(() => {
      this.generateCalendar();
    }, 500)

    // 从缓存加载已保存的文字内容
    // this.loadDateTexts();
  },
});